const App = {
    mixins: [Query],
    data() {
        return {
            page: {
                value1: '',
                value2:'',
                value3:'',
                value4:'',
                PageNumber: 1,
                RowAmount: 10,
                Condition: window.btoa(unescape(encodeURIComponent("%%,%%,%%,%%")))
            },
            treeData: [] // 存储店铺树形数据
        }
    },
    mounted() {
        this.getMerchantTree(); // 获取店铺树形数据
    },
    methods: {
        getList() {
            const that = this;
            const { TableName } = JSON.parse(document.getElementById("table").getAttribute("data-options"));
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                method: "post",
                data: {
                    SQLSerialNumber: 's_base_sql_table_config',
                    Condition: window.btoa(unescape(encodeURIComponent("%" + this.relativePath + "%"))),
                    ExtensionCondition: '',
                },
                success: (data) => {
                    if (data.length > 0) {
                        that.columns = data;
                        that.checkedFields = data;
                        that.fields = data;
                        that.loading = true;
                        // that.postgetList(that.page);
                        
                    } 
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest)
                },
            });
        },
        // 获取店铺树形数据
        getMerchantTree() {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'merchant_business_mapping_blue_plate',
                    Condition: "Row_Status='0'", // 只查询有效店铺
                },
                success: (data) => {//
                    // 构建树形结构数据
                    data.forEach(item =>{
                        item.id = item.Merchant_Mapping_Blue_Plate_SerialNumber;
                        item.name = item.Merchant_Alias
                    })
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                        method: "post",
                        data: {
                            SQLSerialNumber: 'f_merchant_business_scope',
                            Condition: '',
                            ExtensionCondition:window.btoa(unescape(encodeURIComponent("AND merchant_category.Parent_SerialNumber ='MC0000000000012'")))  ,
                        },
                        success: (data1) => {
                            data1.forEach(item=>{
                                item.id = item.Merchant_Business_Scope_SerialNumber;
                                item.name = item.Merchant_Category;
                                item.children = data.filter(i=> i.Merchant_Business_Scope_SerialNumber == item.Merchant_Business_Scope_SerialNumber);
                            })
                            data1 = data1.filter(i=>i.children.length > 0);
                            $.ajax({
                                headers: requestHeaders(),
                                url: `${mh}/generalBusiness/generalQueryByTableName`,
                                type: "POST",
                                data: {
                                    Data: 'merchant',
                                    Condition: "Row_Status='0'", // 只查询有效店铺
                                },
                                success: (data2) => {//
                                    data2.forEach(item=>{
                                        item.id = item.Merchant_SerialNumber;
                                        item.name = item.Merchant_Name;
                                        item.children = data1.filter(i=> i.Merchant_SerialNumber == item.Merchant_SerialNumber);
                                    })
                                    data2 = data2.filter(i=>i.children.length > 0);
                                    this.handleNodeClick(data2[0],{level:1});
                                    this.treeData = data2;
                                },
                                complete: (XMLHttpRequest) => {
                                    requestPretreatment(XMLHttpRequest);
                                },
                                dataType: "json",
                            });
                        },
                        complete: (XMLHttpRequest) => {
                            requestPretreatment(XMLHttpRequest);
                        },
                    })
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        // 树的点击筛选事件
        handleNodeClick(data,node) {
            // 当点击店铺节点时，根据店铺筛选菜品
            this.page.value2 = '';
            this.page.value3 = '';
            this.page.value4 = '';
            if (node.level == 1) {
                this.page.value2 = data.id;
                this.page.Condition = window.btoa(unescape(encodeURIComponent("%"+  this.page.value2 +"%,%%,%%,%%")));
            } else if (node.level == 2) {
                this.page.value3 = data.id;
                this.page.Condition = window.btoa(unescape(encodeURIComponent("%%,%"+  this.page.value3 +"%,%%,%%")));
            } else if (node.level == 3) {
                this.page.value4 = data.id;
                this.page.Condition = window.btoa(unescape(encodeURIComponent("%%,%%,%"+  this.page.value4 +"%,%%")));
            }
            this.postgetList(this.page);
        }, 
        check() {
            this.page.Condition = window.btoa(unescape(encodeURIComponent("%"+this.page.value2+"%,%"+this.page.value3+"%,%"+this.page.value4+"%,%" + this.page.value1 + "%")));
            this.postgetList(this.page);
        },
        handleEdit1(row) {
            window.location.href = './add1.html?action=Modify&nodeKey=' +  row.id;
        },
        personalAdd(){
            window.location.href = './add1.html?action=Add';
        }
    }
};

const { createApp } = Vue;
const app = createApp(App);
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
const ElMessage = ElementPlus.ElMessage;
app.use(ElementPlus, { locale: zhCn });
app.mount("#app");